package com.bit.lotterySystem.common.pojo;

import com.bit.lotterySystem.common.errorcode.ErrorCode;
import com.bit.lotterySystem.common.errorcode.GlobalErrorCodeConstants;
import lombok.Data;
import org.springframework.util.Assert;

import java.io.Serializable;

/**
 * @author wangw
 * @param <T>
 */
@Data
public class CommonResult<T> implements Serializable {
    private Integer code;
    private T data;
    private String msg;

    public static <T> CommonResult<T> success(T data){
        CommonResult<T> result = new CommonResult<T>();
        result.code = 200;
        result.data = data;
        result.msg = "";
        return result;
    }

    public static <T> CommonResult<T> error(Integer code, String msg) {
        Assert.isTrue(!code.equals(GlobalErrorCodeConstants.SUCCESS.getCode()),
                "code不是错误的异常");
        CommonResult<T> result = new CommonResult<>();
        result.code = code;
        result.msg = msg;
        return result;
    }

    public static <T> CommonResult<T> error(ErrorCode errorCode) {
        return error(errorCode.getCode(), errorCode.getErrMsg());
    }

}
